<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 商品卡片 -->
    <el-card>
      <!-- 添加分类 -->
      <el-row>
        <el-col :span="1">
          <el-button @click="addGoodsCate" type="primary">添加分类</el-button>
        </el-col>
        <el-col :span="23"> </el-col>
      </el-row>

      <!-- 分类表格 -->
      <el-table
        :data="goodsCateData"
        style="width: 100%; margin-bottom: 20px"
        row-key="cat_id"
        border
      >
        <el-table-column type="index" label="索引" width="50px">
          <template slot-scope="scope">
            <div>
              {{ scope.row.index }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效">
          <template slot-scope="scope">
            <i v-if="scope.row.cat_deleted" class="el-icon-error"></i>
            <i v-else class="el-icon-success"></i>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template slot-scope="scope">
            <el-tag v-show="scope.row.cat_level == 0">一级</el-tag>
            <el-tag v-show="scope.row.cat_level == 1" type="success"
              >二级</el-tag
            >
            <el-tag v-show="scope.row.cat_level == 2" type="warning"
              >三级</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 编辑对话框 -->
      <el-dialog title="编辑分类" :visible.sync="dialogEdit">
        <el-form ref="editRef" :model="addGoodsFormData">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input
              v-model="addGoodsFormData.cat_name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEdit = false">取 消</el-button>
          <el-button type="primary" @click="editOk">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 添加分类对话框 -->
      <el-dialog title="添加分类" :visible.sync="dialogAdd">
        <el-form :model="addGoodsFormData">
          <!-- 分类名称 -->
          <el-form-item label="分类名称">
            <el-input
              v-model="addGoodsFormData.cat_name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <!-- 级联框 -->
          <el-form-item label="分类名称">
            <el-cascader
              ref="goodsTwoCateRef"
              v-model="checkArr"
              :options="levelTwoData"
              :props="defaultProps"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAdd = false">取 消</el-button>
          <el-button type="primary" @click="addCategoodsOk">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  getGoodsCateList,
  goodsCateEdit,
  goodsCateDel,
  addGoodsCate
} from '@/http/api'
export default {
  name: 'goodsCate',
  components: {},
  data() {
    return {
      current_cat_id: '', //当前分类id
      // 添加分类对话框表单数据源
      addGoodsFormData: {
        cat_pid: '', //分类父id
        cat_name: '', //分类名称
        cat_level: '' //分类层级
      },
      checkArr: [], //级联框选中的id数组
      levelTwoData: [], //二级分类数据源
      // 配置级联框
      defaultProps: {
        label: 'cat_name',
        children: 'children',
        value: 'cat_id',
        expandTrigger: 'hover',
        //实现任意级别的选择
        checkStrictly: true
      },
      dialogEdit: false, //编辑对话框的状态
      dialogAdd: false, //添加分类对话框的状态
      goodsCateData: [], //表格数据
      queryInfo: {
        type: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created() {
    this.getGoodsCateData()
  },
  mounted() {},
  methods: {
    // 添加商品分类
    async addGoodsCate() {
      this.dialogAdd = true //显示对话框
      // 获取级联选择器数据
      const res = await getGoodsCateList({ type: 2 })
      this.levelTwoData = res
    },
    // 添加商品分类完成
    async addCategoodsOk() {
      // 判断是否选中级联框中的数据
      if (this.checkArr.length !== 0) {
        // 父id
        this.addGoodsFormData.cat_pid = this.checkArr[this.checkArr.length - 1]
        // 层级
        this.addGoodsFormData.cat_level = this.checkArr.length
      } else {
        this.addGoodsFormData.cat_pid = 0
        this.addGoodsFormData.cat_level = 0
      }
      // 调用添加分类接口
      const res = await addGoodsCate(this.addGoodsFormData)
      console.log(res)
      // 隐藏添加对话框
      this.dialogAdd = false
      // 刷新分类列表
      this.getGoodsCateData()
    },
    handleChange() {
      //改变选中项时，立马隐藏当前联级选择器
      this.$refs.goodsTwoCateRef.dropDownVisible = false
    },
    // 分页
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsCateData()
    },
    // 分页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsCateData()
    },
    // 获取商品分类数据
    async getGoodsCateData() {
      const res = await getGoodsCateList(this.queryInfo)
      console.log(res)
      const { total, result } = res
      result.forEach((item, index) => {
        item.index = index + 1
      })
      this.total = total
      this.goodsCateData = result
    },

    // 编辑功能
    edit(row) {
      console.log(row)
      this.dialogEdit = true //显示对话框
      this.current_cat_id = row.cat_id
      this.addGoodsFormData.cat_name = row.cat_name
    },
    // 编辑完成
    async editOk() {
      this.dialogEdit = false //隐藏对话框
      await goodsCateEdit(this.current_cat_id, this.addGoodsFormData.cat_name)
    },

    // 删除功能
    del(row) {
      console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        // 确定删除 调用接口
        .then(() => {
          goodsCateDel(row.cat_id).then(() => {
            // 提示
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            // 更新列表
            this.getGoodsCateData()
          })
        })
        // 取消删除
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style>
/* 去掉小圆圈 */
.el-radio__inner {
  display: none;
}
.el-radio {
  position: absolute;
  width: 70%;
  height: 30px;
}
</style>
<style scoped lang="scss">
.el-card {
  margin-top: 16px;
}
.el-icon-success {
  color: green;
}
.el-icon-error {
  color: red;
}
.el-table {
  margin-top: 16px;
}
</style>
